package cn.itsource.pethome.product.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.product.constant.ProductConstant;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.query.ProductQuery;
import cn.itsource.pethome.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 产品服务相关控制层
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    /**
     * 根据条件查询分页数据
     * @param productQuery
     * @return
     */
    @PatchMapping
    public PageList<Product> loadAllProduct(@RequestBody ProductQuery productQuery) {
        return productService.loadPageByQuery(productQuery);
    }

    /**
     * 产品服务删除
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            productService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败" +e.getMessage());
        }
    }
    /**
     * 产品服务批量删除
     * @param ids
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/del")//批量删除用数组接收参数
    public AjaxResult delete(@RequestBody Long[] ids){
        try {
            //调用批量删除方法
            productService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!网络繁忙！" +e.getMessage());
        }
    }


    /**
     * 产品服务添加方法
     *
     * @param product 使用map接收前台传递过来的参数
     * @return
     */
    //@RequestMapping(value = "/save",method = RequestMethod.PUT)
    //RequestBody接收一个对象
    @PutMapping
    public AjaxResult save(@RequestBody Product product) {
        try {
       productService.save(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "添加失败" + e.getMessage());
        }
    }
    /**
     * 产品服务上架
     * @param ids
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/put")//批量上架用数组接收参数
    public AjaxResult PutShelf(@RequestBody Long[] ids){
        try {
            //调用批量删除方法
            productService.patchPut(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上架失败!网络繁忙！" +e.getMessage());
        }
    }
    /**
     * 产品服务下架
     * @param ids
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/down")//批量下架用数组接收参数
    public AjaxResult DownShelf(@RequestBody Long[] ids){
        try {
            //调用批量删除方法
            productService.patchDown(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"下架失败!网络繁忙！" +e.getMessage());
        }
    }

    /**
     * 产品修改
     * @param product
     * @return
     */
    //RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Product product){
        try {
            productService.update(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败" +e.getMessage());
        }
    }



    //==========================================一下都是前台的=========================================

    /**
     * 查询前台需要的分页数据
     * @param productQuery
     * @return
     */
    @PatchMapping("/loadAll")
    public PageList<Product> loadAllProductByQuery(@RequestBody ProductQuery productQuery){
        //设置转态为上架的才显示到前台
        productQuery.setState(ProductConstant.PRODUCT_ACTIVE);
        //进行查询
        return productService.loadPageByQuery(productQuery);
    }

    /**
     * 根据id查询服务产品详细信息
     * @return
     */
    @GetMapping("{id}")
    public Product loadProductById(@PathVariable("id") Long id) {

        return productService.loadById(id);
    }






}
